package top.kpromise.fastandroiddev

import android.app.ActivityManager
import android.content.Context
import android.os.StrictMode
import com.tencent.bugly.crashreport.CrashReport
import top.kpromise.ibase.IApplication
import top.kpromise.ibase.config
import top.kpromise.umenganalytics.UmengCallBack

class AppApplication: IApplication() {

    override fun onCreate() {
        super.onCreate()

        UmengCallBack.init("")

        val umengCallBack = UmengCallBack(null)
        config.lifecycleCallback = umengCallBack
        initBugly()
    }

    private fun initBugly() {
        val name = getCurProcessName()
        if (packageName == name) {
            if (BuildConfig.DEBUG) {
                StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build())
                StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build())
            }
            CrashReport.initCrashReport(this)
        }
    }

    private fun getCurProcessName(): String? {
        val pid = android.os.Process.myPid()
        val mActivityManager = getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager
        mActivityManager?: return null
        for (appProcess in mActivityManager.runningAppProcesses) {
            if (appProcess.pid == pid) {
                return appProcess.processName
            }
        }
        return null
    }
}